<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between" align="center">
        <NSpace align="center">
          姓名:
          <NInput style="width: 200rem" placeholder="请输入姓名" v-model:value="paginationReactive.userName" clearable>
          </NInput>
          部门:
          <NSelect style="width: 200rem" placeholder="请选择部门" v-model:value="paginationReactive.deptId" clearable
            :options="bbgDeptList" label-field="name" value-field="value" filterable />
          <NButton type="primary" style="width: 78rem" ghost
            @click="() => ((paginationReactive.page = 1), getSpecialHolidayList())">
            查询
          </NButton>
          <NButton ghost type="primary" style="width: 200rem" @click="resetAllSpecialHoliday">
            重置所有特殊假期
          </NButton>
        </NSpace>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :columns="columns" :data="tableData" :pagination="false"
        :loading="tableLoading" />
    </template>

    <template #pagination>
      <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>

  <NModal :auto-focus="false" v-model:show="modalVisible" @close="modalVisible = false">
    <VDialogContent @close="modalVisible = false" @submit="reset" title="重置提示">
      <div style="width: 300rem; height: 100rem; text-align: center; display: flex;align-items: center;">
        {{ resetContent }}
      </div>
    </VDialogContent>
  </NModal>
</template>
<script setup lang="ts">
import { NSpace, NButton, NDataTable, NPagination, useMessage, NModal, NSelect, NInput } from 'naive-ui'
import { h, reactive, ref, watch, onMounted, onDeactivated } from 'vue'
import { sendRequest } from '@/utils/api'
import { useRoute } from 'vue-router'
import TableView from '@/components/TableView.vue'
import VDialogContent from '@/views/OA/Common/DialogContent.vue'
import { useViewerStore } from '@/views/OA/Common/store'

const useStore = useViewerStore()
const message = useMessage()
const route = useRoute()
const modalVisible = ref(false)

const columns: any = [
  {
    title: '姓名',
    key: 'userName',
    width: 120,
    align: useStore.isCenter
  },
  {
    title: '部门',
    key: 'deptName',
    width: 120,
    align: useStore.isCenter
  },
  {
    title: '剩余育儿假',
    key: 'yej',
    width: 80,
    align: useStore.isCenter
  },
  {
    title: '剩余陪护假',
    key: 'phj',
    width: 80,
    align: useStore.isCenter
  },
  {
    title: '更新时间',
    key: 'updateTime',
    width: 150,
    align: useStore.isCenter
  },
  {
    title: '操作人',
    key: 'updateBy',
    width: 80,
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    align: useStore.isCenter,
    width: 100,
    fixed: 'right',
    render: (row: any) => [
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: async () => {
            resetContent.value = '确认后将重置该用户的育儿假和陪护假，确定重置吗？'
            resetId.value = row.userId
            resetFlat.value = false
            modalVisible.value = true
          }
        },
        {
          default: () => '重置假期'
        }
      )
    ]
  }
]
// 上一页下一页
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  userName: null, //姓名
  deptId: null, //部门id
  onChange: (page: number) => {
    paginationReactive.page = page
    getSpecialHolidayList()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    getSpecialHolidayList()
  }
})
// 重置所有特殊假期
const resetContent = ref()
const resetFlat = ref(true)
const resetAllSpecialHoliday = () => {
  resetContent.value = '确认后将重置所有用户的育儿假和陪护假，确定重置吗？'
  resetFlat.value = true
  modalVisible.value = true
}
// 重置
const resetId = ref()
const reset = async () => {
  modalVisible.value = false
  tableLoading.value = true
  if (resetFlat.value) {
    const [error, res] = await sendRequest({
      module: 'specialHoliday',
      api: 'resetAllSpecialHoliday'
    })
    if (error) return message.error('重置失败'), (tableLoading.value = false)
    if (res.code !== 0) return message.error('重置失败'), (tableLoading.value = false)
  } else {
    const [error, res] = await sendRequest({
      module: 'specialHoliday',
      api: 'resetSpecialHoliday',
      override: {
        data: [resetId.value]
      }
    })
    if (error) return message.error('重置失败'), (tableLoading.value = false)
    if (res.code !== 0) return message.error('重置失败'), (tableLoading.value = false)
  }
  getSpecialHolidayList()
  message.success('重置成功')
}
// 获取列表
const tableData = ref([])
const tableLoading = ref(false)
const getSpecialHolidayList = async () => {
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'specialHoliday',
    api: 'specialHolidayList',
    override: {
      params: {
        page: paginationReactive.page,
        limit: paginationReactive.pageSize,
        userName: paginationReactive.userName,
        deptId: paginationReactive.deptId
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  tableData.value = []
  setTimeout(() => {
    tableData.value = res.data.list
    paginationReactive.itemCount = res.data.totalCount
  }, 0)
}
//部门
const bbgDeptList: any = ref([])
const getBbgDeptList = async () => {
  const [error, res] = await sendRequest({
    module: 'overTime',
    api: 'BbgDeptList'
  })
  if (error) return message.error(error.message)
  bbgDeptList.value = res.data
}

onMounted(() => {
  getBbgDeptList()
})

const keyUpDown = (e: any) => e.key == 'Enter' && (paginationReactive.page = 1, getSpecialHolidayList())
onDeactivated(() => window.removeEventListener('keyup', keyUpDown, false))

watch(
  () => route,
  async () => {
    if (route.path == '/OA/SpecialHoliday/SpecialHoliday') {
      getSpecialHolidayList()
      window.addEventListener('keyup', keyUpDown, false)  // 回车
    }
  },
  { deep: true, immediate: true }
)
</script>
<style scoped lang="scss">
.table {
  padding: 29rem 30rem 42rem;
}
</style>
  